package com.supersit.greensproject.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.entity.DataUpload;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by chen on 2016/7/25.
 */
public class DataUploadAdapter extends BaseAdapter {
    private Context context;
    private List<DataUpload> data;
    private LayoutInflater inflater;
    private String[] areas;

    public DataUploadAdapter(Context context, List<DataUpload> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
        areas = context.getResources().getStringArray(R.array.area);
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_upload_data, null);
            holder.tvUploadQuarter = (TextView) convertView.findViewById(R.id.tv_uqload_quarter);
            holder.tvAera = (TextView) convertView.findViewById(R.id.tv_base_area);
            holder.tvBaseName = (TextView) convertView.findViewById(R.id.tv_base);
            holder.tvOutput = (TextView) convertView.findViewById(R.id.tv_output);
            holder.llUploadItem = (LinearLayout) convertView.findViewById(R.id.ll_upload_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvUploadQuarter.setText(data.get(position).getQuarter());
        holder.tvOutput.setText(data.get(position).getOutput() + "");
        holder.tvAera.setText(areas[data.get(position).getArea()]);
        holder.tvBaseName.setText(data.get(position).getBaseName());
        if (data.get(position).getIsUpload() == 1) {
            holder.llUploadItem.setBackgroundColor(Color.argb(75, 255, 0, 0));
        }else{
            holder.llUploadItem.setBackgroundColor(Color.argb(0, 255, 0, 0));
        }
        return convertView;
    }

    class ViewHolder {
        TextView tvUploadQuarter;
        TextView tvAera;
        TextView tvBaseName;
        TextView tvOutput;
        LinearLayout llUploadItem;
    }

    public void setData(List<DataUpload> data) {
        this.data = data;
    }
}
